//
//  GDDetailsHeadView.m
//  美团试验 (自己)
//
//  Created by 热带雨林 on 17/2/15.
//  Copyright © 2017年 热带雨林. All rights reserved.
//

#import "GDDetailsHeadView.h"
#import "CZAdditions.h"
#import "Masonry.h"
#import "GDDetailsFoodModel.h"

@interface GDDetailsHeadView ()
@property(nonatomic, weak)UILabel *nameLable;

@property(nonatomic, weak)UILabel *scaleLabel;
@property(nonatomic, weak)UILabel *priceLabel;
@property(nonatomic, weak)UILabel *INLabel;
@property(nonatomic, weak)UILabel *inforLabel;
@end

@implementation GDDetailsHeadView

//- (instancetype)init{
//
//    if(self = [super init]){
//        [self setupUI];
//    }
//    return self;
//}

- (void)setupUI{
    self.backgroundColor = [UIColor whiteColor];
    
    UILabel *nameLabel = [UILabel cz_labelWithText:@"" fontSize:14 color:[UIColor blackColor]];
    self.nameLable = nameLabel;
    [self addSubview:nameLabel];
    
    UIButton *button = [UIButton cz_textButton:@"选进购物车" fontSize:11 normalColor:UIControlStateNormal selectedColor:[UIColor redColor]];
    button.layer.cornerRadius = 6;
    button.layer.masksToBounds = YES;

    [button setImage:[UIImage imageNamed:@"btn_add_to_shopping_cart"] forState:UIControlStateNormal];
    
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setBackgroundColor:[UIColor orangeColor]];
    [self addSubview:button];
    
    [button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    
    //月售
    UILabel *scale = [UILabel cz_labelWithText:@"月售 " fontSize:11 color:[UIColor grayColor]];
    self.scaleLabel = scale;
    [self addSubview:scale];
    
    //价格
    UILabel *priceL = [UILabel cz_labelWithText:@"" fontSize:13 color:[UIColor grayColor]];
    self.priceLabel = priceL;
    [self addSubview:priceL];
    
    //中间的横线
    UIView *lineView = [[UIView alloc]init];
    lineView.backgroundColor = [UIColor grayColor];
    [self addSubview:lineView];
    
    //商品简介
    UILabel *INLabel = [UILabel cz_labelWithText:@"商品简介" fontSize:16 color:[UIColor blackColor]];
    self.INLabel = INLabel;
    [self addSubview:INLabel];
    
    UILabel *inforLabel = [UILabel cz_labelWithText:@"" fontSize:10 color:[UIColor grayColor]];
    self.inforLabel = inforLabel;
    [self addSubview:inforLabel];
    
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(50);
        make.width.mas_equalTo(100);
    }];
    
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(50);
        make.right.mas_equalTo(-20);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(30);
    }];
    [scale mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(nameLabel.mas_bottom).offset(30);
        make.left.equalTo(nameLabel);
    }];
    [priceL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(scale.mas_bottom).offset(10);
        make.left.equalTo(nameLabel);
    }];
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(priceL.mas_bottom).offset(20);
        make.left.equalTo(self).offset(10);
        make.right.equalTo(self).offset(-10);
        make.height.mas_equalTo(0.19);
    }];
    
    [INLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lineView).offset(10);
        make.left.equalTo(nameLabel);
    }];
    
    [inforLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(INLabel.mas_bottom).offset(15);
        make.left.equalTo(INLabel);
        make.right.equalTo(self).offset(-100);
    }];
  
}

- (void)setDetailsModel:(GDDetailsFoodModel *)detailsModel{
    
    _detailsModel = detailsModel;
    _nameLable.text = detailsModel.name;
    _scaleLabel.text = detailsModel.min_order_count;
    _priceLabel.text = [NSString stringWithFormat:@"价格:%@",detailsModel.min_price];
    _inforLabel.text = detailsModel.Description;
}

- (void)clickButton:(UIButton *)sender{
    
    //开始执行动画，坐标进行转移
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    
    //创建一个点 按钮的中心
    CGPoint startP = [self convertPoint:sender.center toView:keyWindow];
    
    //创建通知
    NSNotificationCenter *notification = [NSNotificationCenter  defaultCenter];
    
    //创建需要传递的数据
    NSDictionary *dic = @{
                          @"startP":[NSValue valueWithCGPoint:startP],
                          @"model":_detailsModel
                          };
    //发送消息
    [notification postNotificationName:@"kNoticatioPageVBuyCar" object:nil userInfo:dic];
}
@end
